<template>
  <div v-if="coupons && coupons.length" id="goods-coupons">
    <div class="pro-list">
      <div class="pro-title">优&ensp;惠&ensp;券</div>
      <div class="pro-content">
        <template v-for="(coupon, index) in coupons">
          <div
            v-if="index < 2"
            :key="coupon.coupon_id"
            class="coupons-item"
            @click.stop="handleReceiveCoupon(coupon)"
          >
            <i class="coupons-border-l"></i>
            <i class="coupons-border-r"></i>
            <span class="tj">满{{ coupon.coupon_threshold_price }}减{{ coupon.coupon_price }}</span>
          </div>
        </template>
        <a v-if="coupons.length > 2" href="javascript:;" @click.stop="sidebarOpen = true">查看更多>></a>
      </div>
    </div>
    <div :class="['coupons-sidebar', sidebarOpen && 'open']">
      <div class="title-sidebar">
        <i class="icon-title-sidebar icon-coupons"></i>
        <span class="title-title-sidebar">优惠券</span>
        <i class="icon-title-sidebar icon-close" @click.stop="sidebarOpen = false"></i>
      </div>
      <div class="content-sidebar" @click.stop="() => {}">
        <!--可领优惠券-->
        <div class="items-coupons">
          <div class="title-items-coupons">
            <i class="line line-l"></i>
            <span class="title-span">可领优惠券</span>
            <i class="line line-r"></i>
          </div>
          <div class="content-items-coupons">
            <div v-for="coupon in coupons" :key="coupon.coupon_id" class="item-b">
              <div class="detail-b">
                <div class="money-b">
                  <i class="arrow sysicon">￥</i>
                  <span class="money">{{ coupon.coupon_price | unit_price('', 'before') }}</span>
                  <i class="arrow">{{ coupon.coupon_price | unit_price('', 'after') }}</i>
                </div>
                <div class="other-b">
                  <p>{{ coupon.title }}</p>
                  <p class="color-gary">满{{ coupon.coupon_threshold_price }}减{{ coupon.coupon_price }}</p>
                  <p class="color-gary">
                    有效期{{ coupon.start_time | unixToDate('yyyy-MM-dd') }} 至 {{ coupon.end_time | unixToDate('yyyy-MM-dd') }}
                  </p>
                </div>
              </div>
              <div class="btn-b">
                <a href="javascript:;" @click.stop="handleReceiveCoupon(coupon)" class="get-b is-get">立即领取</a>
                <i class="is-get-b"></i>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  /**
   * 商品优惠券模块
   * 查看店铺优惠券列表、领取优惠券
   */
  import * as API_Members from '@/api/members'
  import * as API_Promotions from '@/api/promotions'
  import Storage from '@/utils/storage'
  export default {
    name: 'goods-coupons',
    props: ['goods', 'isSnapshot', 'selectedSku'],
    data() {
      return {
        sidebarOpen: false,
        couponsAll: [],
        coupons: []
      }
    },
    mounted() {
      if (this.isSnapshot) {
        this.coupons = this.goods.coupon_list
      } else {
        this.GET_CouponsList()
      }
      this.$nextTick(() => {
        window.addEventListener('click', this.handleClsoeSide)
      })
    },
    watch: {
      selectedSku: function (val) {
        if (!this.couponsAll || !this.couponsAll.length) return
        const sku_coupons = this.couponsAll.find(key => key.sku_id === val.sku_id).coupon_list
        this.coupons = sku_coupons
      }
    },
    methods: {
      /** 获取优惠券列表 */
      GET_CouponsList() {
        const { goods_id } = this.goods
        API_Promotions.getOwnCoupons(goods_id).then(response => {
          this.couponsAll = response
        })
      },
      /** 关闭优惠券侧边栏 */
      handleClsoeSide() {
        this.sidebarOpen = false
      },
      /** 领取优惠券 */
      handleReceiveCoupon(coupon) {
        if (this.isSnapshot) {
          this.$message.error('此优惠券为此商品的快照信息，不能领取！')
          return false
        }
        if (!Storage.getItem('refresh_token')) {
          this.$confirm('您还未登录，要现在去登录吗？', () => {
            this.$router.push({ path: `/login?forward=${this.$route.path}` })
          })
          return false
        }
        API_Members.receiveCoupons(coupon.coupon_id).then(() => {
          this.$message.success('领取成功！')
        })
      }
    },
    destroyed() {
      window.removeEventListener('click', this.handleClsoeSide)
    }
  }
</script>

<style type="text/scss" lang="scss" scoped>
  @import "../../assets/styles/color";
  .coupons-item {
    position: relative;
    display: inline-block;
    height: 16px;
    padding-left: 2px;
    line-height: 16px;
    text-align: center;
    border-top: 1px solid #df3033;
    border-bottom: 1px solid #df3033;
    background: #ffdedf;
    font-size: 14px;
    white-space: nowrap;
    margin-right: 13px;
    cursor: pointer;
  }

  .coupons-item .tj {
    padding: 0 10px;
    color: #df3033;
    font-size: 12px;
  }

  .coupons-item .coupons-border-l,
  .coupons-item .coupons-border-r {
    position: absolute;
    top: -1px;
    display: block;
    height: 18px;
    width: 2px;
    overflow: hidden;
    background: url('') no-repeat;
  }

  .coupons-item .coupons-border-l {
    left: 0;
    background-position: -2px 0;
  }

  .coupons-item .coupons-border-r {
    right: -2px;
  }

  .coupons-more {
    cursor: pointer;
    font-size: 12px;
  }

  .coupons-sidebar {
    position: fixed;
    top: 0;
    right: -300px;
    padding-top: 50px;
    width: 280px;
    height: 100%;
    z-index: 10000;
    overflow-x: hidden;
    overflow-y: scroll;
    background-color: #f5f5f5;
    box-shadow: 0 0 15px #ccc;
    transition: all .3s ease-out;
  }

  .coupons-sidebar::-webkit-scrollbar {
    width: 5px;
  }

  .coupons-sidebar::-webkit-scrollbar-track {
    background-color: #f5f5f5;
  }

  .coupons-sidebar::-webkit-scrollbar-thumb {
    background-color: #7f7f7f;
  }

  .coupons-sidebar::-webkit-scrollbar-thumb:hover {
    background-color: #666
  }

  .coupons-sidebar::-webkit-scrollbar-thumb:active {
    background-color: #666
  }

  .coupons-sidebar.open {
    right: 0;
  }

  .title-sidebar {
    position: fixed;
    z-index: 10;
    top: 0;
    right: -300px;
    width: 280px;
    height: 40px;
    line-height: 40px;
    background-color: #f5f5f5;
    box-shadow: 0 0 15px #ccc;
    transition: all .3s ease-out;
  }

  .coupons-sidebar.open .title-sidebar {
    right: 0;
  }

  .icon-title-sidebar {
    display: block;
    width: 40px;
    height: 40px;
  }

  .title-title-sidebar {
    float: left;
    font-size: 14px;
  }

  .icon-title-sidebar.icon-coupons {
    float: left;
    background: url('') no-repeat center;
    background-size: 60%;
  }

  .icon-title-sidebar.icon-close {
    float: right;
    background: url('') no-repeat center;
    background-size: 30%;
    margin-right: 10px;
    cursor: pointer;
    transition: all .3s ease-out;
  }

  .icon-title-sidebar.icon-close:hover {
    transform: rotate(180deg);
  }

  .content-sidebar {
    padding: 0 10px 50px 10px;
  }

  .items-coupons {
    clear: both;
  }

  .title-items-coupons {
    position: relative;
    text-align: center;
  }

  .title-items-coupons .line {
    position: absolute;
    top: 50%;
    display: block;
    margin-top: -1px;
    width: 33%;
    height: 1px;
    background-color: #afafaf;
  }

  .line.line-l {
    left: 0;
  }

  .line.line-r {
    right: 0;
  }

  .title-items-coupons .title-span {
    font-size: 12px;
    color: #333;
  }

  .content-items-coupons .item-b {
    position: relative;
    width: 255px;
    margin: 20px 0;
    height: 106px;
    background-color: #fff;
    overflow: hidden;
  }

  .item-b .detail-b {
    position: relative;
    float: left;
    width: 230px;
    height: 106px;
    background-color: #fff;
    overflow: hidden;
  }

  .item-b .btn-b {
    position: relative;
    float: right;
    width: 20px;
    height: 106px;
    background-color: #ff002d;
    color: #fff;
  }

  .item-b .btn-b:before {
    content: "";
    position: absolute;
    top: -20px;
    display: block;
    width: 10px;
    height: 100%;
    margin-top: 20px;
    background-size: 20px 6px;
    left: -10px;
    background-color: #fff;
    background-position: 100% 35%;
    background-image: linear-gradient(-45deg, #ff002d 25%, transparent 25%, transparent),
    linear-gradient(-135deg, #ff002d 25%, transparent 25%, transparent),
    linear-gradient(-45deg, transparent 75%, #ff002d 75%),
    linear-gradient(-135deg, transparent 75%, #ff002d 75%);
  }

  .item-b .get-b {
    display: block;
    width: 14px;
    font-size: 13px;
    color: #fff;
    padding: 12px 2px;
  }

  .item-b .get-b:hover {
    color: #fece75;
  }

  .item-b .money-b {
    width: 225px;
    font-family: Arial;
    font-size: 35px;
    font-weight: 700;
    line-height: 24px;
    padding-top: 10px;
    color: #ff002d;
  }

  .money-b .arrow {
    font-size: 16px;
    font-weight: normal;
  }

  .money-b .sysicon {
    margin-left: 10px;
  }

  .item-b .other-b {
    padding-left: 15px;
    font-size: 12px;
  }

  .item-b .other-b p {
    line-height: 15px;
  }

  .item-b .other-b .color-gary {
    color: #999;
  }

  .item-b .is-get-b {
    position: absolute;
    bottom: 5px;
    right: 35px;
    display: none;
    width: 50px;
    height: 50px;
    background: url('') no-repeat center;
    background-size: 100%;
  }
</style>
